<?php

namespace App\Http\Controllers\Kms\Backend\Attendance;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Kms\Attendance\AttendanceValidate;
use App\Service\Kms\Attendance\AttendanceService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class AttendanceController extends BaseController
{

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function fieldGroupList(Request $request): JsonResponse
    {
        $params = AttendanceValidate::fieldGroupListValidate($request->all());
        return $this->success(AttendanceService::fieldGroupList($params));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function dailyFieldGroupList(Request $request): JsonResponse
    {
        $params = AttendanceValidate::fieldGroupListValidate($request->all());
        return $this->success(AttendanceService::dailyFieldGroupList($params));
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function fieldList(Request $request): JsonResponse
    {
//        $pageSize  = (int)$request->get("pageSize", 10);
//        $pageNum   = (int)$request->get("pageNum", 1);
//        $uid       = (int)$request->get("uid", 0);
//        $startDate = (int)$request->get("startDate", 0);
//        $endDate   = (int)$request->get("endDate", 0);
//        $status    = $request->get("status", null);

        $params = AttendanceValidate::fieldListValidate($request->all());
        return $this->success(AttendanceService::fieldList($params));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function fieldTraceDetailList(Request $request): JsonResponse
    {
        $params = AttendanceValidate::fieldTraceListValidate($request->all());
        return $this->success(AttendanceService::fieldTraceDetailList($params));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function fieldTrace(Request $request): JsonResponse
    {
        $params = AttendanceValidate::fieldTraceListValidate($request->all());
        return $this->success(AttendanceService::fieldTraceV3($params));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function fieldSwitch(Request $request): JsonResponse
    {
        $params = AttendanceValidate::fieldSwitchValidate($request->all());
        AttendanceService::fieldSwitch(
            $params->status,
            $params->departmentId ?? "",
            $params->jobTitle ?? "",
            $params->name ?? "",
            (property_exists($params, "fieldSwitch") && strlen($params->fieldSwitch) > 0) ? $params->fieldSwitch : null,
            $params->uid ?? []
        );
        return $this->success();
    }


}
